package dk.mil.triton.hest;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * A view that draws a graph with an upper and lower value
 */
public class IntervalView extends View {
    private int min;
    private int max;
    private static Paint redPaint, yellowPaint, greenPaint;

    static {
        redPaint = new Paint();
        redPaint.setARGB(255, 255, 0, 0);
        redPaint.setStyle(Paint.Style.FILL);
        yellowPaint = new Paint(redPaint);
        yellowPaint.setARGB(255,255,255,0);
        greenPaint = new Paint(yellowPaint);
        greenPaint.setARGB(255,0,255,0);
    }

    public IntervalView(Context context) {
        super(context);
    }

    public IntervalView(Context context, AttributeSet attrs) {
        super(context, attrs);    //To change body of overridden methods use File | Settings | File Templates.
    }

    public IntervalView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);    //To change body of overridden methods use File | Settings | File Templates.
    }

    public void updateLimits(int min, int max) {
        this.min = min;
        this.max = max;
        invalidate();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        int width = getWidth();
        int height = getHeight();

        int pTop = getPaddingTop();
        int pBottom = getPaddingBottom();
        int pLeft = getPaddingLeft();
        int pRight = getPaddingRight();

        int aWidth = width - pLeft - pRight;
        int aHeight = height - pTop - pBottom;

        if (min < max) {
            //draw the red background
            canvas.drawRect(pLeft, pTop, aWidth, aHeight, greenPaint);

            //draw the yellow interval
            int startX = (min * aWidth)/100;
            int endX = (max * aWidth)/100;
            canvas.drawRect( pLeft + startX,pTop,endX,aHeight,yellowPaint);
        } else if (min == max) {
            canvas.drawRect(pLeft, pTop, aWidth, aHeight, redPaint);
        }

        super.onDraw(canvas);
    }
}
